package org.example.ui.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.application.UrlGroupService;
import org.example.context.SecurityContextHolder;
import org.example.domain.model.entity.url.req.UrlShortenerGroupSortReq;
import org.example.domain.model.entity.url.req.UrlShortenerUpdateGroupReq;
import org.example.web.result.ResultInfo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/domain/v1/group")
@Tag(name = "短链接分组", description = "短链接分组")
@RequiredArgsConstructor
public class UrlGroupController {

    private final UrlGroupService urlGroupService;

    @GetMapping("/save")
    @Operation(summary = "新增短链接分组")
    public ResultInfo<Void> saveGroup(@RequestParam String groupName) {
        String userName = SecurityContextHolder.getUserName();
        urlGroupService.saveGroup(groupName, userName);
        return ResultInfo.success();
    }


    @GetMapping("/selectList")
    @Operation(summary = "查询短链接分组集合")
    public ResultInfo listGroup() {
        String userName = SecurityContextHolder.getUserName();
        return ResultInfo.success(urlGroupService.listGroup(userName));
    }


    @PutMapping("/updateGroup")
    @Operation(summary = "修改短链接分组名称")
    public ResultInfo<Void> updateGroup(@RequestBody UrlShortenerUpdateGroupReq req) {
        String userName = SecurityContextHolder.getUserName();
        urlGroupService.updateGroup(req, userName);
        return ResultInfo.success();
    }

    @DeleteMapping("/deleteGroup")
    @Operation(summary = "删除短链接分组")
    public ResultInfo<Void> deleteGroup(@RequestParam String gid) {
        String userName = SecurityContextHolder.getUserName();
        urlGroupService.deleteGroup(gid, userName);
        return ResultInfo.success();
    }


    @PostMapping("/sortGroup")
    @Operation(summary = "排序短链接分组")
    public ResultInfo<Void> sortGroup(@RequestBody List<UrlShortenerGroupSortReq> reqList) {
        String userName = SecurityContextHolder.getUserName();
        urlGroupService.sortGroup(reqList, userName);
        return ResultInfo.success();
    }

    @GetMapping("/selectGitList")
    @Operation(summary = "查询短链接分组gid")
    public ResultInfo selectGitListByUserName(@RequestParam String userName) {
        return ResultInfo.success(urlGroupService.selectGitListByUserName(userName));
    }
}